---
layout: "default"
title: "Unicode.UTF32"
description: "Swift documentation for enum 'Unicode.UTF32'."
keywords: "Unicode.UTF32,enum,swift,documentation,decode,decode,encode,encode,encodedReplacementCharacter,CodeUnit,EncodedScalar,ForwardParser,ReverseParser"
root: "/v4.2"
---

<div class="intro-declaration"><code class="language-swift">enum Unicode.UTF32</code></div>


<table class="standard">
<tr>
<th id="inheritance">Inheritance</th>
<td>
<code class="inherits">UnicodeCodec, _UnicodeEncoding</code>
<span class="viz"><a href="hierarchy/">View Protocol Hierarchy &rarr;</a></span>
</td>
</tr>

<tr>
<th id="aliases">Associated Types</th>
<td>
<span id="aliasesmark"></span>
<div class="declaration">
<code class="language-swift">CodeUnit = UInt32</code>
<div class="comment">
    <p>The basic unit of encoding</p>
</div>
</div>
<div class="declaration">
<code class="language-swift">EncodedScalar = CollectionOfOne&lt;UInt32&gt;</code>
<div class="comment">
    <p>A valid scalar value as represented in this encoding</p>
</div>
</div>
<div class="declaration">
<code class="language-swift">ForwardParser = Unicode.UTF32.Parser</code>
<div class="comment">
    <p>A type that can be used to parse <code>CodeUnits</code> into
<code>EncodedScalar</code>s.</p>
</div>
</div>
<div class="declaration">
<code class="language-swift">ReverseParser = Unicode.UTF32.Parser</code>
<div class="comment">
    <p>A type that can be used to parse a reversed sequence of
<code>CodeUnits</code> into <code>EncodedScalar</code>s.</p>
</div>
</div>
</td>
</tr>

<tr>
<th>Nested Types</th>
<td><code class="nested">Unicode.UTF32.Parser</code></td>
</tr>

<tr>
<th>Import</th>
<td><code class="language-swift">import Swift</code></td>
</tr>

</table>

<h3 id="cases">Cases</h3>
<div class="declaration" id="case-_swift3Codec">
<a class="toggle-link" data-toggle="collapse" href="#comment-case-_swift3Codec">case _swift3Codec</a>
</div>

<h3>Initializers</h3>
<div class="declaration" id="init">
<a class="toggle-link" data-toggle="collapse" href="#comment-init">init()</a><div class="comment collapse" id="comment-init"><div class="p">
    <p>Creates an instance of the UTF-32 codec.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">init()</code>

    </div></div>
</div>

<h3>Static Variables</h3>
<div class="declaration" id="static-var-encodedreplacementcharacter_-unicode-utf32-encodedscalar">
<a class="toggle-link" data-toggle="collapse" href="#comment-static-var-encodedreplacementcharacter_-unicode-utf32-encodedscalar">static var encodedReplacementCharacter: Unicode.UTF32.EncodedScalar</a><div class="comment collapse" id="comment-static-var-encodedreplacementcharacter_-unicode-utf32-encodedscalar"><div class="p">
    <p>A unicode scalar value to be used when repairing
encoding/decoding errors, as represented in this encoding.</p>

<p>If the Unicode replacement character U+FFFD is representable in this
encoding, <code>encodedReplacementCharacter</code> encodes that scalar value.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static var encodedReplacementCharacter: Unicode.UTF32.EncodedScalar { get }</code>

    </div></div>
</div>



<h3>Static Methods</h3>
<div class="declaration" id="func-decode_-unicode-utf32-encodedscalar">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-decode_-unicode-utf32-encodedscalar">static func decode(<wbr>_: Unicode.UTF32.EncodedScalar)</a>
        
<div class="comment collapse" id="comment-func-decode_-unicode-utf32-encodedscalar"><div class="p">
    <p>Converts from encoded to encoding-independent representation</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static func decode(_ source: Unicode.UTF32.EncodedScalar) -&gt; Unicode.Scalar</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-encode_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-encode_">static func encode(<wbr>_:)</a>
        
<div class="comment collapse" id="comment-func-encode_"><div class="p">
    <p>Converts from encoding-independent to encoded representation, returning
<code>nil</code> if the scalar can&#39;t be represented in this encoding.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static func encode(_ source: Unicode.Scalar) -&gt; Unicode.UTF32.EncodedScalar?</code>
    
    
</div></div>
</div>
<div class="declaration" id="func-encode_into_">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-encode_into_">static func encode(<wbr>_:<wbr>into:)</a>
        
<div class="comment collapse" id="comment-func-encode_into_"><div class="p">
    <p>Encodes a Unicode scalar as a UTF-32 code unit by calling the given
closure.</p>

<p>For example, like every Unicode scalar, the musical fermata symbol (&quot;𝄐&quot;)
can be represented in UTF-32 as a single code unit. The following code
encodes a fermata in UTF-32:</p>

<pre><code class="language-swift">var codeUnit: UTF32.CodeUnit = 0
UTF32.encode(&quot;𝄐&quot;, into: { codeUnit = $0 })
print(codeUnit)
// Prints &quot;119056&quot;</code></pre>

<p><strong>Parameters:</strong>
  <strong>input:</strong> The Unicode scalar value to encode.
  <strong>processCodeUnit:</strong> A closure that processes one code unit argument at a
    time.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">static func encode(_ input: Unicode.Scalar, into processCodeUnit: (Unicode.UTF32.CodeUnit) -&gt; Void)</code>
    
    
</div></div>
</div>

<h3>Instance Methods</h3>
<div class="declaration" id="func-decode-i_-inout-i">
<a class="toggle-link" data-toggle="collapse" href="#comment-func-decode-i_-inout-i">mutating func decode&lt;I&gt;(<wbr>_: inout I)</a>
        
<div class="comment collapse" id="comment-func-decode-i_-inout-i"><div class="p">
    <p>Starts or continues decoding a UTF-32 sequence.</p>

<p>To decode a code unit sequence completely, call this method repeatedly
until it returns <code>UnicodeDecodingResult.emptyInput</code>. Checking that the
iterator was exhausted is not sufficient, because the decoder can store
buffered data from the input iterator.</p>

<p>Because of buffering, it is impossible to find the corresponding position
in the iterator for a given returned <code>Unicode.Scalar</code> or an error.</p>

<p>The following example decodes the UTF-16 encoded bytes of a string
into an array of <code>Unicode.Scalar</code> instances. This is a demonstration
only---if you need the Unicode scalar representation of a string, use
its <code>unicodeScalars</code> view.</p>

<pre><code class="language-swift">// UTF-32 representation of &quot;✨Unicode✨&quot;
let codeUnits: [UTF32.CodeUnit] =
        [10024, 85, 110, 105, 99, 111, 100, 101, 10024]

var codeUnitIterator = codeUnits.makeIterator()
var scalars: [Unicode.Scalar] = []
var utf32Decoder = UTF32()
Decode: while true {
    switch utf32Decoder.decode(&amp;codeUnitIterator) {
    case .scalarValue(let v): scalars.append(v)
    case .emptyInput: break Decode
    case .error:
        print(&quot;Decoding error&quot;)
        break Decode
    }
}
print(scalars)
// Prints &quot;[&quot;\u{2728}&quot;, &quot;U&quot;, &quot;n&quot;, &quot;i&quot;, &quot;c&quot;, &quot;o&quot;, &quot;d&quot;, &quot;e&quot;, &quot;\u{2728}&quot;]&quot;</code></pre>

<p><strong><code>input</code>:</strong>  An iterator of code units to be decoded. <code>input</code> must be
  the same iterator instance in repeated calls to this method. Do not
  advance the iterator or any copies of the iterator outside this
  method.
<strong>Returns:</strong> A <code>UnicodeDecodingResult</code> instance, representing the next
  Unicode scalar, an indication of an error, or an indication that the
  UTF sequence has been fully decoded.</p>

    <h4>Declaration</h4>    
    <code class="language-swift">mutating func decode&lt;I&gt;(_ input: inout I) -&gt; UnicodeDecodingResult where I : IteratorProtocol, I.Element == Unicode.UTF32.CodeUnit</code>
    
    
</div></div>
</div>


